package com.itheima.travel.service.impl;

import com.itheima.travel.constant.RedisConstant;
import com.itheima.travel.mapper.CategoryMapper;
import com.itheima.travel.pojo.Category;
import com.itheima.travel.pojo.CategoryExample;
import com.itheima.travel.req.CategoryVo;
import com.itheima.travel.service.CategoryService;
import com.itheima.travel.service.RedisCacheService;
import com.itheima.travel.session.SubjectUserContext;
import com.itheima.travel.utils.BeanConv;
import com.itheima.travel.utils.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/*分类服务*/
@Service
public class CategoryServiceImpl implements CategoryService {
   @Autowired
   private CategoryMapper categoryMapper;
   @Autowired
   private RedisCacheService redisCacheService;
   @Autowired
   private SubjectUserContext subjectUserContext;
    //查询所有分类
    @Override
    public List<CategoryVo> findAllCategory() {
        String key = RedisConstant.CATEGORYSERVICE_FINDALLCATEGORY;
        //将结果放缓存里面
        return redisCacheService.listCache(() -> {
            //创建一个例子，就是条件
            CategoryExample categoryExample = new CategoryExample();
            //根据条件查询所有信息
            List<Category> categories = categoryMapper.selectByExample(categoryExample);
            if (!EmptyUtil.isNullOrEmpty(categories)) {
                //有结果的话返回结果，并且转为vo类型展示出来
                return BeanConv.toBeanList(categories, CategoryVo.class);
            }
            return null;
        }, key);
    }
}
